<?php
include_once('common/header.php');
if($AppUI->id <=0 || ($AppUI->id > 0 && $AppUI->type < 2) || (isset($_SESSION['admin']) && ($_SESSION['admin']&1<<2)==0)) {
	$this->_redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
$arrParams 		= $objRequest->getParams();
$intAdminId		= isset($arrParams['padminid']) ? $arrParams['padminid'] : 0;
$strUsername	= isset($arrParams['pusername']) ? $arrParams['pusername'] : '';
$objAdmin 		= new Admin();
$arrSection		= $objAdmin->getSection();
$arrPerms		= array();
$intError		= 0;
if($arrSection) {
	foreach($arrSection as $row) {
		$arrPerms[$row['sectionid']] = Ecore_Function::convertArrToInt($arrParams['pPerm'.$row['sectionid']]);
	}
}
if($arrPerms) {	
	$intError	= $objAdmin->deleteAdPermission($AppUI->id, $intAdminId);	
	if($intError == 0) {
		foreach($arrPerms as $key=>$value) {
			if($value > -1) {
				$intError = $objAdmin->addAdPermission($AppUI->id, $intAdminId, $key, $value);					
				if($intError > 0) {
					echo $intError;exit();
					break;
				}
			}
		}
	}
}
if($intError > 0) {
	Ecore_Function::setError($arrError['no_permission']);
} else {
	$strMessage	= $arrMessage['permission_sucessfully'];
	$strMessage	= str_replace('[strUsername]', $strUsername, $strMessage);
	Ecore_Function::setMsg($strMessage);
}
$this->_redirect(HOST_BACKEND . 'permission?adminid=' .$intAdminId);
